package com.qf.controller;

import com.qf.service.GoodsService;
import com.qf.service.OrderService;
import com.qf.service.ProductService;
import com.qf.service.UserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

//首页
@RestController
@CrossOrigin
@RequestMapping("/admin/dashboard")
public class AdminController {

    @Resource
    private UserService userService;

    @Resource
    private GoodsService goodsService;

    @Resource
    private ProductService productService;

    @Resource
    private OrderService orderService;

    //统计的天数范围
    private static final int STATIS_DAYS_RANG = 30;

    /**
     * {
     * "errno":0,
     * "data":{
     * "goodsTotal":426,
     * "userTotal":197,
     * "productTotal":8939,
     * "orderTotal":107
     * },
     * "errmsg":"成功"
     * }
     * 首页数据
     *
     * @return
     */
    @RequestMapping
    public Object dashBoard() {
        int goodsTotal = goodsService.count();
        int userTotal = userService.count();
        int productTotal = productService.count();
        int orderTotal = orderService.count();

        HashMap<String, Integer> result = new HashMap<>();

        result.put("userTotal", userTotal);
        result.put("goodsTotal", goodsTotal);
        result.put("productTotal", productTotal);
        result.put("orderTotal", orderTotal);

        return ResponseUtil.ok(result);
    }


    /**
     * 首页数据可视化
     * {
     *     "errno":0,
     *     "data":{
     *         "categorySell":{
     *             "categoryNames":["数码家电","日用百货","鞋帽箱包"],
     *             "categorySellData":[
     *                 {"name":"数码家电","value":68.76},
     *                 {"name":"日用百货","value":48.76},
     *                 {"name":"鞋帽箱包","value":277.53}
     *             ]
     *         },
     *         "orderAmts":{
     *             "dayData":[
     *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
     *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
     *                 "2119-12-11","2119-12-13","2119-12-15","2119-12-16",
     *                 "2119-12-17","2119-12-19","2119-12-20","2119-12-23",
     *                 "2119-12-25","2119-12-27","2119-12-28","2119-12-29",
     *                 "2119-12-30"
     *             ],
     *             "orderAmtData":[
     *                 6.88,138.79,277.65,65.64,182.00,61.00,
     *                 289.01,375.76,29.50,247.50,145.50,
     *                 31.18,399.44,16.99,150.01,218.65,255.77,
     *                 126.59,275.25,97.74,14.63
     *             ],
     *             "orderCntData":[
     *                 1,2,4,1,3,1,2,3,1,1,16,3,9,2,1,2,2,2,2,2,2
     *             ]
     *         },
     *         "userOrderCnt":{
     *             "dayData":[
     *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
     *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
     *                 "2119-12-09","2119-12-10","2119-12-11","2119-12-12",
     *                 "2119-12-13","2119-12-14","2119-12-15","2119-12-16",
     *                 "2119-12-17","2119-12-18","2119-12-19","2119-12-20",
     *                 "2119-12-21","2119-12-22","2119-12-23","2119-12-24",
     *                 "2119-12-25","2119-12-26","2119-12-27","2119-12-28",
     *                 "2119-12-29","2119-12-30"
     *             ],
     *             "userCnt":[
     *                 5,9,13,7,6,2,1,3,17,6,2,9,8,17,23,8,
     *                 4,8,8,14,3,2,1,1,2,1,2,7,3,5
     *             ],
     *             "orderCnt":[
     *                 1,2,4,1,3,1,2,3,0,0,1,0,1,0,16,3,9,
     *                 0,2,1,0,0,2,0,2,0,2,2,2,2
     *             ]
     *         }
     *     },
     *     "errmsg":"成功"
     * }
     * @return
     */
    @RequestMapping("/chart")
    public Object chart(){

        /**
         *  "categorySell":{
         *             "categoryNames":["数码家电","日用百货","鞋帽箱包"],
         *             "categorySellData":[
         *                 {"name":"数码家电","value":68.76},
         *                 {"name":"日用百货","value":48.76},
         *                 {"name":"鞋帽箱包","value":277.53}
         *             ]
         *         },
         *
         *
         *  统计各个类的销售情况
         */
        //查询销售情况    string类型 跟  BigDecimal 类型   categorySellData
        List<CategorySellAmts> categorySellData = orderService.categorySell();

        //提取name
        int size = 0;
        if (categorySellData != null && categorySellData.size() > 0) {
            size = categorySellData.size();
        }

        String[] categoryNames = new String[size];
        for (int i = 0; i < size; i++) {
            categoryNames[i] = categorySellData.get(i).getName();
        }

        CategorySellVo categorySell = new CategorySellVo();
        categorySell.setCategoryNames(categoryNames);
        categorySell.setCategorySellData(categorySellData);


        /**
         * "orderAmts":{
         *             "dayData":[
         *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
         *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
         *                 "2119-12-11","2119-12-13","2119-12-15","2119-12-16",
         *                 "2119-12-17","2119-12-19","2119-12-20","2119-12-23",
         *                 "2119-12-25","2119-12-27","2119-12-28","2119-12-29",
         *                 "2119-12-30"
         *             ],
         *             "orderAmtData":[
         *                 6.88,138.79,277.65,65.64,182.00,61.00,
         *                 289.01,375.76,29.50,247.50,145.50,
         *                 31.18,399.44,16.99,150.01,218.65,255.77,
         *                 126.59,275.25,97.74,14.63
         *             ],
         *             "orderCntData":[
         *                 1,2,4,1,3,1,2,3,1,1,16,3,9,2,1,2,2,2,2,2,2
         *             ]
         *         },
         *
         *
         *  统计一个月内销售额 订单数
         */


        //统计三十天内订单增长量
        List<DayStatis> orderCnts = orderService.recentCount(STATIS_DAYS_RANG);

        //订单统计   订单数  订单金额统计
        OrderAmtsVo orderAmts = new OrderAmtsVo();

        /**
         * 	private String[] dayData;//日期数据
         *
         * 	private BigDecimal[] orderAmtData;//日订单金额
         *
         * 	private int[] orderCntData;//日订单量
         */
        size = 0;
        if (orderCnts != null && orderCnts.size() > 0){
            size = orderCnts.size();
        }
        String [] orderData = new String[size];
        BigDecimal [] orderAmtData = new BigDecimal[size];
        int [] orderCntData = new int[size];

        for (int i = 0; i < size; i++) {
            orderData[i] = orderCnts.get(i).getDayStr();
            orderAmtData[i] = orderCnts.get(i).getAmts();
            orderCntData[i] = orderCnts.get(i).getCnts();
        }

        orderAmts.setDayData(orderData);
        orderAmts.setOrderAmtData(orderAmtData);
        orderAmts.setOrderCntData(orderCntData);

        /**
         * "userOrderCnt":{
         *             "dayData":[
         *                 "2119-12-01","2119-12-02","2119-12-03","2119-12-04",
         *                 "2119-12-05","2119-12-06","2119-12-07","2119-12-08",
         *                 "2119-12-09","2119-12-10","2119-12-11","2119-12-12",
         *                 "2119-12-13","2119-12-14","2119-12-15","2119-12-16",
         *                 "2119-12-17","2119-12-18","2119-12-19","2119-12-20",
         *                 "2119-12-21","2119-12-22","2119-12-23","2119-12-24",
         *                 "2119-12-25","2119-12-26","2119-12-27","2119-12-28",
         *                 "2119-12-29","2119-12-30"
         *             ],
         *             "userCnt":[
         *                 5,9,13,7,6,2,1,3,17,6,2,9,8,17,23,8,
         *                 4,8,8,14,3,2,1,1,2,1,2,7,3,5
         *             ],
         *             "orderCnt":[
         *                 1,2,4,1,3,1,2,3,0,0,1,0,1,0,16,3,9,
         *                 0,2,1,0,0,2,0,2,0,2,2,2,2
         *             ]
         *
         *  统计近日用户数和订单数增长情况
         */
        //统计用户增长量
        List<DayStatis> userCnts = userService.recentCount(STATIS_DAYS_RANG);
        // 统计时间    set 过滤重复时间
        HashSet<String> days = new HashSet<>();
        for (DayStatis userCnt : userCnts) {
            days.add(userCnt.getDayStr());
        }
        for (DayStatis orderCnt : orderCnts) {
            days.add(orderCnt.getDayStr());
        }

        ArrayList<String> list = new ArrayList<>(days);
        //排序  从小到大
        Collections.sort(list);
        //转换数组形式
        String[] dayData = list.toArray(new String[0]);


        /**
         * 	//日期数据
         * 	private String[] dayData;
         *
         * 	//每日用户新增量
         * 	private int[] userCnt;
         *
         * 	//每日订单量
         * 	private int[] orderCnt;
         */
        UserOrderCntVo userOrderCnt = new UserOrderCntVo();


        userOrderCnt.setDayData(dayData);
        userOrderCnt.setOrderCnt(fetchArrCnt(dayData, orderCnts));
        userOrderCnt.setUserCnt(fetchArrCnt(dayData, userCnts));

        /**
         * 封装入json数据串中
         */

        HashMap<String, Object> data = new HashMap<>();
        data.put("categorySell", categorySell);
        data.put("orderAmts", orderAmts);
        data.put("userOrderCnt",userOrderCnt);
        return ResponseUtil.ok(data);
    }

    //补充缺失数据为 0
    private int[] fetchArrCnt(String[] dayData,List<DayStatis> dayStatis){
        int[] data = new int[dayData.length];
        for (int i = 0; i < dayData.length; i++) {
            int dayCnt = 0;
            String dayStr = dayData[i];
            for (DayStatis dayStati : dayStatis) {
                if (dayStr.equals(dayStati.getDayStr())){
                    dayCnt = dayStati.getCnts();
                    break;
                }
            }
            data[i] = dayCnt;
        }
        return data;
    }

}
